# coding=utf-8
import unittest
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.rent_order import RentOrder
from api.base_api.rent_order_waiting import WaitingRentOrder
from api.base_api.rent_order_cancel import RentOrderCancel
import datetime
import random


class RentOrderCancelTest(LoginBaseApi):
    ''' 测试发布租车 '''

    def setUp(self):
        self.cancel_rent_order_url = "http://192.168.1.141:8001/rent-order/id/cancel"
        self.rent_order_url = "http://192.168.1.141:8001/rent-order"
        self.renting_contract_mobile = '15011353321'
        self.none_contract_mobile = '13651020524'
        self.start_time = datetime.datetime.now()
        self.end_time = self.start_time + datetime.timedelta(hours=random.randint(1, 240))
        self.rent_day = (self.end_time - self.start_time).days
        self.rent_amount = random.randint(1, 200)
        self.unit_price = self.rent_day * self.rent_amount
        self.password = '123abc'

    def test_cancel_rent_order_success(self):
        ''' 取消发布租车成功 '''

        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        payload = RentOrder().rent_order(str(self.start_time), str(self.end_time), self.rent_day, self.rent_amount,
                                         self.unit_price)
        response = self.post(self.rent_order_url, payload, header)
        status_code = response.status_code
        self.assertEqual(status_code, 201)
        rent_order_id = response.json()['id']

        # 发布租车后获取租车列表
        waiting_rent_order_response = WaitingRentOrder().waiting_rent_order()
        rent_order_list_amount = len(waiting_rent_order_response.json()['results'])

        #调用取消租车接口
        new_url = self.cancel_rent_order_url.replace("id", str(rent_order_id))
        payload = RentOrderCancel().rent_order_cancel()
        cancel_rent_order_response = self.post( new_url, payload, header)
        self.assertEqual(cancel_rent_order_response.status_code, 200)

        # 验证租车列表数量
        new_waiting_rent_order_response = WaitingRentOrder().waiting_rent_order()
        new_rent_order_list_amount = len(new_waiting_rent_order_response.json()['results'])
        self.assertEqual(rent_order_list_amount, new_rent_order_list_amount + 1)

        #列表找不到ID
        new_rent_order_id = new_waiting_rent_order_response.json()['results']
        self.assertNotIn(rent_order_id, new_rent_order_id)

    def test_already_cancel_rent_order(self):
        ''' 已经取消订单 '''
        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        new_url = self.cancel_rent_order_url.replace("id", str(308))
        payload = RentOrderCancel().rent_order_cancel()
        cancel_rent_order_response = self.post(new_url, payload, header)
        self.assertNotEqual(cancel_rent_order_response.status_code, 200)
        message = cancel_rent_order_response.json()['message']
        self.assertEqual('该订单不能取消', message)

    def test_not_exist_rent_order(self):
        ''' 订单不存在 '''
        header = LoginBaseApi().login_base_api(self.renting_contract_mobile, self.password)
        new_url = self.cancel_rent_order_url.replace("id", str(100021))
        payload = RentOrderCancel().rent_order_cancel()
        cancel_rent_order_response = self.post(new_url, payload, header)
        self.assertNotEqual(cancel_rent_order_response.status_code, 200)
        detail = cancel_rent_order_response.json()['detail']
        self.assertEqual('Not found.', detail)